home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap09 / Paint7 / Paint7View.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  2.7 KB  |  114 lines

  1. //***********************************************************************
  2. //
  3. //  Paint7View.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "CLine.h"
  10. #include "Paint7Doc.h"
  11. #include "Paint7View.h"
  12.  
  13. IMPLEMENT_DYNCREATE (CPaintView, CScrollView)
  14.  
  15. BEGIN_MESSAGE_MAP (CPaintView, CScrollView)
  16.     ON_WM_LBUTTONDOWN ()
  17.     ON_WM_MOUSEMOVE ()
  18.     ON_WM_LBUTTONUP ()
  19.     ON_WM_CONTEXTMENU ()
  20. END_MESSAGE_MAP ()
  21.  
  22. void CPaintView::OnInitialUpdate ()
  23. {
  24.     SetScrollSizes (MM_TEXT, CSize (2048, 2048));
  25.     CScrollView::OnInitialUpdate ();
  26. }
  27.  
  28. void CPaintView::OnUpdate (CView* pSender, LPARAM lHint, CObject* pHint)
  29. {
  30.     if (pHint != NULL) {
  31.         CClientDC dc (this);
  32.         OnPrepareDC (&dc);
  33.         ((CLine*) pHint)->Draw (&dc);
  34.         return;
  35.     }
  36.     CScrollView::OnUpdate (pSender, lHint, pHint);
  37. }
  38.  
  39. void CPaintView::OnDraw (CDC* pDC)
  40. {
  41.     CPaintDoc* pDoc = GetDocument ();
  42.     int nCount = pDoc->GetLineCount ();
  43.  
  44.     if (nCount) {
  45.         for (int i=0; i<nCount; i++)
  46.             pDoc->GetLine (i)->Draw (pDC);
  47.     }
  48. }
  49.  
  50. void CPaintView::OnLButtonDown (UINT nFlags, CPoint point)
  51. {
  52.     CClientDC dc (this);
  53.     OnPrepareDC (&dc);
  54.     dc.DPtoLP (&point);
  55.  
  56.     m_ptFrom = point;
  57.     m_ptTo = point;
  58.     SetCapture ();
  59. }
  60.  
  61. void CPaintView::OnMouseMove (UINT nFlags, CPoint point)
  62. {
  63.     if (GetCapture () == this) {
  64.         CClientDC dc (this);
  65.         OnPrepareDC (&dc);
  66.         dc.DPtoLP (&point);
  67.  
  68.         InvertLine (&dc, m_ptFrom, m_ptTo);
  69.         InvertLine (&dc, m_ptFrom, point);
  70.         m_ptTo = point;
  71.     }
  72. }
  73.  
  74. void CPaintView::OnLButtonUp (UINT nFlags, CPoint point)
  75. {
  76.     if (GetCapture () == this) {
  77.         ReleaseCapture ();
  78.         CClientDC dc (this);
  79.         OnPrepareDC (&dc);
  80.         dc.DPtoLP (&point);
  81.         InvertLine (&dc, m_ptFrom, m_ptTo);
  82.  
  83.         CLine* pLine = GetDocument ()->AddLine (m_ptFrom, point);
  84.         if (pLine != NULL) {
  85.             pLine->Draw (&dc);
  86.             GetDocument ()->UpdateAllViews (this, 0, pLine);
  87.         }
  88.     }
  89. }
  90.  
  91. void CPaintView::InvertLine (CDC* pDC, CPoint ptFrom, CPoint ptTo)
  92. {
  93.     int nOldMode = pDC->SetROP2 (R2_NOT);
  94.  
  95.     pDC->MoveTo (ptFrom);
  96.     pDC->LineTo (ptTo);
  97.  
  98.     pDC->SetROP2 (nOldMode);
  99. }
  100.  
  101. void CPaintView::OnContextMenu (CWnd* pWnd, CPoint point)
  102. {
  103.     CMenu menu;
  104.     menu.LoadMenu (IDR_CONTEXTMENU);
  105.     CMenu* pContextMenu = menu.GetSubMenu (0);
  106.  
  107.     for (int i=0; i<8; i++)
  108.         pContextMenu->ModifyMenu (ID_COLOR_BLACK + i,
  109.             MF_BYCOMMAND | MF_OWNERDRAW, ID_COLOR_BLACK + i);
  110.  
  111.     pContextMenu->TrackPopupMenu (TPM_LEFTALIGN | TPM_LEFTBUTTON |
  112.         TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd ());
  113. }
  114.